#!/bin/bash

# 时间格式化
TIME_TAG=`date "+%Y%m%d%H%M"`
# Docker 私有仓库地址
IMAGE_REPOSITORY=192.168.0.224:8060/$1/

# git提交的版本
GIT_VERSION=`git log -1 --pretty=format:"%h"`


AUTH_MODULES=(maynrent-admin-auth maynrent-web-auth)
GATE_MODULES=(maynrent-admin-gate maynrent-web-gate)
SVC_MODULES=(maynrent-crm maynrent-finance maynrent-market maynrent-mc-service maynrent-order maynrent-sys maynrent-product maynrent-user maynrent-wms)
THIRD_MODULES=(maynrent-third maynrent-trade maynrent-report)
MIDDLE_MODULES=(maynrent-daemon)

echo "(1)开始构建鉴权模块......"

for moduleName in ${AUTH_MODULES[@]};do
	echo "start build $moduleName...."
	cd maynrent-auth/$moduleName

	echo "Dockerfile所在路径：" && pwd

	IMAGE_NAME=${IMAGE_REPOSITORY}$moduleName:${TIME_TAG}_${GIT_VERSION}

	echo "开始构建镜像： ${IMAGE_NAME}"
	docker build -t ${IMAGE_NAME} .

	echo "开始推送镜像:${IMAGE_NAME}至私有仓库：${IMAGE_REPOSITORY}"
	docker push ${IMAGE_NAME}

	echo "删除当前镜像：${IMAGE_NAME}"
	docker rmi ${IMAGE_NAME}

	cd ../../
	#将构建好的镜像名称存到本地，后面发布的时候会用到
	echo "${IMAGE_NAME}" > $2/image_$moduleName

	echo "build $moduleName end!!!"
done

echo "(2)开始构建网关模块......"
for moduleName in ${GATE_MODULES[@]};do
	cd maynrent-gate/$moduleName

	echo "Dockerfile所在路径：" && pwd

	IMAGE_NAME=${IMAGE_REPOSITORY}$moduleName:${TIME_TAG}_${GIT_VERSION}

	echo "开始构建镜像： ${IMAGE_NAME}"
	docker build -t ${IMAGE_NAME} .

	echo "开始推送镜像:${IMAGE_NAME}至私有仓库：${IMAGE_REPOSITORY}"
	docker push ${IMAGE_NAME}

	echo "删除当前镜像：${IMAGE_NAME}"
	docker rmi ${IMAGE_NAME}

	cd ../../
	#将构建好的镜像名称存到本地，后面发布的时候会用到
	echo "${IMAGE_NAME}" > $2/image_$moduleName

	echo "build $moduleName end!!!"
done

echo "(3)开始构建基础服务模块......"
for moduleName in ${SVC_MODULES[@]};do

	cd maynrent-modules/$moduleName

	echo "Dockerfile所在路径：" && pwd

	IMAGE_NAME=${IMAGE_REPOSITORY}$moduleName:${TIME_TAG}_${GIT_VERSION}

	echo "开始构建镜像： ${IMAGE_NAME}"
	docker build -t ${IMAGE_NAME} .

	echo "开始推送镜像:${IMAGE_NAME}至私有仓库：${IMAGE_REPOSITORY}"
	docker push ${IMAGE_NAME}

	echo "删除当前镜像：${IMAGE_NAME}"
	docker rmi ${IMAGE_NAME}

	cd ../../
	#将构建好的镜像名称存到本地，后面发布的时候会用到
	echo "${IMAGE_NAME}" > $2/image_$moduleName

	echo "build $moduleName end!!!"
done

echo "(4)开始构建第三方服务模块......"
for moduleName in ${THIRD_MODULES[@]};do
	cd $moduleName

	echo "Dockerfile所在路径：" && pwd

	IMAGE_NAME=${IMAGE_REPOSITORY}$moduleName:${TIME_TAG}_${GIT_VERSION}

	echo "开始构建镜像： ${IMAGE_NAME}"
	docker build -t ${IMAGE_NAME} .

	echo "开始推送镜像:${IMAGE_NAME}至私有仓库：${IMAGE_REPOSITORY}"
	docker push ${IMAGE_NAME}

	echo "删除当前镜像：${IMAGE_NAME}"
	docker rmi ${IMAGE_NAME}

	cd ../

	#将构建好的镜像名称存到本地，后面发布的时候会用到
	echo "${IMAGE_NAME}" > $2/image_$moduleName

	echo "build $moduleName end!!!"
done

echo "(5)开始构建中间件服务模块......"
for moduleName in ${MIDDLE_MODULES[@]};do

	cd maynrent-middleware/$moduleName

	echo "Dockerfile所在路径：" && pwd

	IMAGE_NAME=${IMAGE_REPOSITORY}$moduleName:${TIME_TAG}_${GIT_VERSION}

	echo "开始构建镜像： ${IMAGE_NAME}"
	docker build -t ${IMAGE_NAME} .

	echo "开始推送镜像:${IMAGE_NAME}至私有仓库：${IMAGE_REPOSITORY}"
	docker push ${IMAGE_NAME}

	echo "删除当前镜像：${IMAGE_NAME}"
	docker rmi ${IMAGE_NAME}

	cd ../../
	#将构建好的镜像名称存到本地，后面发布的时候会用到
	echo "${IMAGE_NAME}" > $2/image_$moduleName

	echo "build $moduleName end!!!"
done

echo "所有镜像构建、推送完毕..."
